package cn.shengchao.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.shengchao.examstar.distribute.infrastrure.po.DistributeNodeInfo;
import cn.shengchao.examstar.distribute.infrastrure.query.SaleAmountQuery;
import cn.shengchao.examstar.distribute.infrastrure.service.DistributeNodeInfoService;
import cn.shengchao.examstar.distribute.infrastrure.service.DistributeOrderTaskSaleService;
import cn.shengchao.examstar.distribute.infrastrure.vo.DistributeSale;
import cn.shengchao.examstar.rpc.distribute.enums.DistributeSaleRoleEnum;
import cn.shengchao.examstar.product.acl.ports.repositories.ICourseRepository;
import cn.shengchao.examstart.user.acl.adapters.message.MemberStudent;
import cn.shengchao.examstart.user.ohs.local.appservices.UserAppService;
import cn.shengchao.examstart.user.ohs.local.messages.UserInfoDto;
import cn.shengchao.examstar.product.acl.ports.repositories.message.MemberCourseOrder;
import cn.shengchao.service.vo.MemberVo;
import cn.shengchao.util.helper.LoginHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MemberAppService {

    @Autowired
    private DistributeNodeInfoService distributeNodeInfoService;
    @Autowired
    private UserAppService userAppService;
    @Autowired
    private DistributeOrderTaskSaleService distributeOrderTaskSaleService;
    @Autowired
    private ICourseRepository iCourseRepository;

    /**
     * 会员信息
     */
    public MemberVo getMemberInfo(Long userId) {
        DistributeNodeInfo distributeNodeInfo = distributeNodeInfoService.getDistributeNodeInfo(LoginHelper.getUserId(), DistributeSaleRoleEnum.ROLE_SLMB);
        if (distributeNodeInfo != null) {
            // 会员信息
            MemberVo memberVo = new MemberVo();
            // 用户信息
            UserInfoDto userInfo = userAppService.getUserInfo(userId);
            // 会员信息
            memberVo.setNickName(userInfo.getNickName());
            // 头像
            memberVo.setHeadImageUrl(userInfo.getAvatarUrl());
            // 有效期
            memberVo.setValidStartTime(distributeNodeInfo.getValidStartTime());
            // 有效期
            memberVo.setValidEndTime(distributeNodeInfo.getValidEndTime());
            SaleAmountQuery saleAmountQuery = new SaleAmountQuery();
            saleAmountQuery.setDistributeUserId(distributeNodeInfo.getUserId());
            // 总收益
            DistributeSale amount = distributeOrderTaskSaleService.getSaleAmount(saleAmountQuery);
            if (amount != null) {
                memberVo.setIncome(amount.getAmount());
            }
            saleAmountQuery = new SaleAmountQuery();
            saleAmountQuery.setDistributeUserId(distributeNodeInfo.getUserId());
            saleAmountQuery.setStartSaleTime(DateUtil.beginOfMonth(new Date()));
            saleAmountQuery.setEndSaleTime(new Date());
            // 本月收益
            amount = distributeOrderTaskSaleService.getSaleAmount(saleAmountQuery);
            if (amount != null) {
                memberVo.setMonthIncome(amount.getAmount());
            }
            saleAmountQuery = new SaleAmountQuery();
            saleAmountQuery.setDistributeUserId(distributeNodeInfo.getUserId());
            saleAmountQuery.setStatus(List.of("0", "1", "2"));
            // 待结算
            amount = distributeOrderTaskSaleService.getSaleAmount(saleAmountQuery);
            if (amount != null) {
                memberVo.setPending(amount.getAmount());
            }
            return memberVo;

        }
        return new MemberVo();
    }


    public List<MemberCourseOrder> getMemberCourse(Long userId, String courseName, String sort) {
        return iCourseRepository.getMemberCourse(userId, courseName, sort);
    }

    public List<MemberStudent> getMemberStudent(Long userId, String searchValue) {
        return userAppService.selectMemberStudent(userId, searchValue).stream().peek(item -> item.setPhone(PhoneUtil.hideBetween(item.getPhone()).toString())).toList();
    }
}
